import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:jl_plugin/jl_plugin.dart';
import 'package:jl_plugin_example/BluetoothWakeupTask2.dart';
import 'package:jl_plugin_example/home.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'BluetoothHelper.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

void main() {

  runApp(const MyApp());
  initSmartDialog();
  FlutterForegroundTask.init(
    androidNotificationOptions: AndroidNotificationOptions(
      channelId: 'my_channel_id',
      channelName: 'Foreground Service',
      channelDescription: '执行定时任务',
      channelImportance: NotificationChannelImportance.LOW,
      priority: NotificationPriority.LOW,
    ),
    iosNotificationOptions: const IOSNotificationOptions(),
    foregroundTaskOptions:  ForegroundTaskOptions(
      interval: 1000,
      autoRunOnBoot: false,
      autoRunOnMyPackageReplaced: false,
      allowWakeLock: true,
      allowWifiLock: true,
    ),
  );
}

@pragma('vm:entry-point')
void startCallback() {
  FlutterForegroundTask.setTaskHandler(BluetoothWakeupTask2());
}

Future<ServiceRequestResult> startService() async {
  if (await FlutterForegroundTask.isRunningService) {
    print("isRunningService");
    return FlutterForegroundTask.restartService();
  } else {
    print("startServicestartServicestartService");
    return FlutterForegroundTask.startService(
      notificationTitle: 'Foreground Service is running',
      notificationText: 'Tap to return to the app',
      notificationIcon: null,
      callback: startCallback,
    );
  }
}
void initSmartDialog(){
  SmartDialog.config
    ..custom = SmartConfigCustom(
      maskColor: Colors.black.withOpacity(0.35),
      useAnimation: true,
    )
    ..attach = SmartConfigAttach(
      animationType: SmartAnimationType.scale,
      usePenetrate: false,
    )
    ..loading = SmartConfigLoading(
      clickMaskDismiss: false,
      backDismiss: true,
      leastLoadingTime: const Duration(milliseconds: 0),
    )
    ..notify =SmartConfigNotify(
      displayTime: const Duration(milliseconds: 1300),
      maskColor: Colors.black.withOpacity(0.5),  // 设置背景色和透明度
    )
    ..toast = SmartConfigToast(
      intervalTime: const Duration(milliseconds: 100),
      displayTime: const Duration(milliseconds: 1300),
      maskColor: Colors.black.withOpacity(0.5),  // 设置背景色和透明度
    );
}
class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context,child){
        return Stack(children: [
          FlutterSmartDialog.init()(context,child),
        ],);
      },
      theme: ThemeData(
        textButtonTheme: TextButtonThemeData(
          style: ButtonStyle(
            foregroundColor: MaterialStateProperty.all(Colors.white),  // 字体颜色
            backgroundColor: MaterialStateProperty.all(Colors.blue),   // 背景色
            padding: MaterialStateProperty.all(
              EdgeInsets.symmetric(horizontal: 20, vertical: 12),
            ),
            shape: MaterialStateProperty.all(
              RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
            ),
            textStyle: MaterialStateProperty.all(
              TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
          ),
        ),
      ),
      home: HomePage(),
    );
  }

}
